<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Forum/classes/PhpFox_ComponentPosts.class.php, $Author:  $ */



/** Forums list componment
 *
 * 1. oUser (optional) - current user object; for visitor - null or omitted
 *
 * @package module.forum
 * @version $Id: PhpFox_ComponentPosts.class.php 482 2008-08-15 11:29:00Z phpfoxsv $
 *
 */
class PhpFox_Mod_Forum_ComponentPosts extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
		$oSrvAcc = &App::getModuleService('Account', 'Account');
                        
        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
        $oSrvSec->checkPerm('forum', array('read_only', 'full'));

	    $oSrvForum = &$this->_oModule->getService('Forum');
	    /* @var $oSrvForum PhpFox_Mod_Forum_ServiceForum */

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

	    $iTopicId = $oReq->getInt('id');
	    $oTopic = $this->_oModule->getItem('ForumTopic');
	    if (! $oTopic-> load($iTopicId))
	    {
	        App::gotoUrl('public.forum');
	    }


	    $bIsStaff = $oSrvSec->isStaff();

	    $oUser = $this->getParam('oUser');
	    if (!$oUser)
	    {
	        $oSrvSec = App::getModuleService('Account', 'Security');
	        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */

	        $oUser = &$oSrvSec->getCurrentUser();
	    }



        switch($oReq->getAction())
        {
            case 'forum_topic_delete':
                if ($bIsStaff)
                {
                    $oTopic = $this->_oModule->getItem('ForumTopic');
                    if ($oTopic->load($oReq->getInt('id')))
                    {
                        $iForumId = $oTopic->aData['type'];
                        $oSrvForum->deleteTopic($oTopic);
                        $oSrvAcc->changeUserInfo($oSrvAcc->getByUsername($oTopic->aData['user']), 'forum', -1);
                        App::gotoUrl('public.forum.topics', array('id'=>$iForumId));
                    }
                }

                App::gotoUrl('public.forum');
                break;

            case 'forum_post_delete':
                $oPost = $this->_oModule->getItem('ForumReply');
                if ($oPost->load($oReq->getInt('del')))
                {
                    if ($bIsStaff || ($oUser && ($oPost->aData['user'] == $oUser->aData['user'])))
                    {
                        $iTopicId = $oPost->aData['fid'];
                        $oPost->delete($oPost->getId());
                        $oSrvAcc->changeUserInfo($oSrvAcc->getByUsername($oPost->aData['user']), 'forum', -1);
                        $oSrvForum->updateTopicStats($iTopicId);
                    }
                    $oUrl = App::getUrl();
                    $oUrl->clearParams('act');
                    $oUrl->clearParams('del');
                    App::forward($oUrl->getUrl(), App::format('Mod_Forum.post_deleted'), 'message',
                                 App::getPageName(), 'ForumPosts');
                }

                App::forward('public.forum');
                break;
        }

		if ('last' != $oReq->get('page'))
		{
    	    $iPage = $oReq->getInt('page', 1);
		}
    	else
    	{
    	    $iPage = 9999999; //to get last page
    	}


    	$iPageSize = App::getParam('public.pagesize');

        $aCond = array('fid' => ' = '.$iTopicId);
	    list($aPosts, $iTotal) = $oSrvForum->getPostsList($aCond, $iPage, $iPageSize, '`time`');

	    $oSrvAcc = App::getModuleService('Account', 'Account');
	    /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */

	    foreach($aPosts as $k=>$v)
	    {
	        $aPosts[$k]['memb_image'] = $oSrvAcc->getMembershipImageByType($v['user_type']);
	        $aPosts[$k]['stars_info'] = $oSrvAcc->getStarsAsArray($v['user_points']);
	        $aPosts[$k]['can_edit']   = $bIsStaff || (isset($oUser->aData) and $aPosts[$k]['user'] == $oUser->aData['user']);
	        $aPosts[$k]['can_delete']   = $bIsStaff || (isset($oUser->aData) and $aPosts[$k]['user'] == $oUser->aData['user']);
	    }
	    $oPager = App::newObject('Pager', $iTotal, $iPage, $iPageSize);

	    $aTopic = null;

	    //if ((1 == $iPage) || ('last' == $oReq->get('page')))
	    //{
        //first page - load info on topic starting post

        $oTopic = &$this->_oModule->getItem('ForumTopic');
        /* @var $oTopic PhpFox_Mod_Forum_ItemForumTopic */
        /*
        if ($oTopic->load($oReq->getInt('id')))
        {
            $aTopic = $oTopic->aData;
            $oSrvAcc = &App::getModuleService('Account', 'Account');
            if ($oAcc = $oSrvAcc->getByUsername($oTopic->aData['user']))
            {
                $aTopic['memb_image'] = $oSrvAcc->getMembershipImage($oAcc);
                $aTopic['stars_info'] = $oSrvAcc->getPointsStars($oAcc);
                $aTopic = array_merge($aTopic, $oSrvForum->getTopicAddInfo($oTopic));
            }
        }
        */
	    //}
	    $oTopic->aData = $this->oDatabase->getRow("
	    	SELECT t.`id`, t.`type`, t.`user`, t.`title`, t.`text`, t.`view`, t.`forum_type`, t.`time`, t.total_replies, u.`type` AS user_type, u.`headline` AS user_headline, p.total AS user_points FROM
	    	`". App::getT('forum_topic') ."` AS t
	    		LEFT JOIN `". App::getT('user') ."` AS u
	    		ON t.user = u.user
	    		LEFT JOIN ". App::getT('user_info') ." AS p
	    		ON p.user = u.user
	    	WHERE t.`id` = '". $oReq->getInt('id') ."'
	    ");
	    
	    $aTopic = $oTopic->aData;
	    if(count($aTopic)==0)
	    {
	    	 App::gotoUrl('public.forum');
	    }
	    $aTopic['stars_info'] = $oSrvAcc->getStarsAsArray($aTopic['user_points']);

	    $aTopic['memb_image'] = $oSrvAcc->getMembershipImageByType($aTopic['user_type']);

	    if ($oTopic)
	    {
	        $oSrvForum->addTopicView($oTopic);
	    }

        if ($oUser)
        {
            $oSrvForum->markTopicView($oTopic, $oUser);
        }

	    $oSrvSec = &App::getModuleService('Account', 'Security');
	    /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */

	    $oTpl = &$this->_oModule->getTemplate();

		$oTpl->assignSrc(array(
    		'aPosts'      => $aPosts,
		    'aTopic'      => $aTopic,
    		'sUserPicUrl' => App::getParam('user.pic.url'),
	        'aPaging'     => $oPager->getInfo(App::getUrl()),
		    'bIsAdmin'    => $oSrvSec->isStaff(),
            'dtToday'     => time(),
            'iTopicId'    => $iTopicId,
            'bTopicAuthor' => ($oUser && ($oUser->aData['user']==$oTopic->aData['user']))

		));
		$srcHtml = $oTpl->fetch('Posts.html');
		return $srcHtml;
	}

}
?>